<template>
  <!--委外诉讼案件-->
  <section class="page company-manage">
    <data-form :model="queryModel" @onSearch="refreshData" :page="pageService" @handleResetForm="follFeedback">
      <template slot="inputs">
        <el-form-item label="借据号" prop="caseNumber">
          <el-input v-model="queryModel.caseNumber"></el-input>
        </el-form-item>
        <el-form-item label="客户姓名" prop="personalName">
          <el-input v-model="queryModel.personalName"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobileNo">
          <el-input v-model="queryModel.mobileNo"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="queryModel.idCard"></el-input>
        </el-form-item>
        <el-form-item label="案件金额" prop="overdueAmount">
          <number-range v-model="queryModel.overdueAmount">
          </number-range>
        </el-form-item>
        <el-form-item label="逾期阶段" prop="overduePeriod" >
          <el-select v-model="queryModel.overduePeriod" placeholder="所有" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('OverduePeriodStatus')" :key="index" :label="name" :value="code.replace(/M|\+/, '')"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="逾期天数" prop='overdueDays'>
          <number-range v-model="queryModel.overdueDays"></number-range>
        </el-form-item>
        <el-form-item label="系统来源" prop="sysCode">
          <el-select v-model="queryModel.sysCode" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('SysCode')" :key="index" :label="name" :value="code.replace(/M|\+/, '')"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品名称" prop='productName'>
          <el-input v-model="queryModel.productName"></el-input>
        </el-form-item>
      </template>
    </data-form>
    <data-box @onPageChange="refreshData" :selectionList.sync="selectionList" :dataList.sync="dataList" :data="dataList" :page="pageService" >
      <template slot="columns">
        <el-table-column label="操作" :min-width="$helper.getColumnWidth(6)">
          <template slot-scope="scope">
            <el-button v-if="scope.row.assistStatus !== 'ASSIST_OVER'" type="text" @click="isView=false,lawsuitClick(scope.row)">诉讼跟进</el-button>
            <el-button v-if="scope.row.assistStatus === 'ASSIST_OVER' && scope.row.lawerStatus && scope.row.lawsuitFollowType" type="text" @click="isView=true,lawsuitClick(scope.row)">跟进查看</el-button>
            <el-button type="text" @click="rowData=scope.row, dialog.isOpenCostMaintenance=true">费用维护</el-button>
          </template>
        </el-table-column>
        <el-table-column  label="借据号" :min-width="$helper.getColumnWidth(8)">
          <template slot-scope="scope">
              <a  @click="openExecutionPage(scope.row)">{{scope.row.caseNumber}}</a>
            </template>
        </el-table-column>
        <el-table-column prop="lawNumber" label="诉讼编号" :min-width="$helper.getColumnWidth(4)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="deptName" label="律所" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="personalName" label="客户名称" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="mobileNo" label="手机号" :formatter="(row)=>$filter.encryptPhone(row.mobileNo)" :min-width="$helper.getColumnWidth(4)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="idCard" label="身份证号" :formatter="(row)=>$filter.encryptIDCardFour(row.idCard)" :min-width="$helper.getColumnWidth(8)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="localAddress" label="地区" :min-width="$helper.getColumnWidth(4)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="overdueAmount" label="案件金额" :min-width="$helper.getColumnWidth(4)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="overduePeriod" label="逾期阶段" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="overdueDays" label="逾期天数" :min-width="$helper.getColumnWidth(4)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="sysCode" label="系统来源" :formatter="(row) => $filter.encryptSysCode(row.sysCode)" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="productName" label="产品名称" :min-width="$helper.getColumnWidth(4)" show-overflow-tooltip>
        </el-table-column>
        <!-- <el-table-column prop="customerCategory" label="顾客类型" :min-width="$helper.getColumnWidth(4)" :formatter="(row) => $filter.dictConvert(row.customerCategory, 'CustomerCategory')">
        </el-table-column> -->
        <el-table-column prop="assistCollectorName" label="跟进人员" :min-width="$helper.getColumnWidth(4)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="createTime" label="案件创建时间" :min-width="$helper.getColumnWidth(6)">
          <template slot-scope="scope">
            <span>{{scope.row.createTime | dateTimeFormat}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="lawerStatus" label="诉讼状态" :formatter="(row) => $filter.dictConvert(row.lawerStatus,'LawerStatus')" :min-width="$helper.getColumnWidth(4)" show-overflow-tooltip>
        </el-table-column>
      </template>
    </data-box>

    
    <!-- 诉讼跟进-未确定诉讼类型的情况 -->
    <el-dialog title="诉讼跟进" center :visible.sync="dialog.isOpenLawsuitKind" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false">
      <lawsuit-kind ref="lawsuit-kind" @close="dialog.isOpenLawsuitKind = false" :rowData="rowData" :lawsuit="lawsuit" @refreshData="refreshData"></lawsuit-kind>
    </el-dialog>

    <!-- 诉讼跟进-已经确定诉讼类型的情况 -->
    <el-dialog title="诉讼跟进" center :visible.sync="dialog.isOpenLawsuitFollow" append-to-body width="900px" @open="() => this.$nextTick(() => this.$refs['lawsuit-follow'].refresh(rowData.lawerStatus))" @close="refreshData">
      <lawsuit-follow ref="lawsuit-follow" @close="dialog.isOpenLawsuitFollow = false" @refreshData="refreshData" :lawsuit="lawsuit" :rowData="rowData" :isView="isView"></lawsuit-follow>
    </el-dialog>

    <!-- 费用管理 -->
    <el-dialog title="费用管理" :center="true" :visible.sync="dialog.isOpenCostMaintenance" width="1200px" @close="refreshData" @open="() => this.$nextTick(() => this.$refs['cost'].refreshCostData())">
      <cost-maintain ref="cost" :rowData="rowData" @close="dialog.isOpenCostMaintenance = false" @refreshData="refreshData" ></cost-maintain>
    </el-dialog>
  </section>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Auth, Layout, Dependencies } from "~/core/decorator";
import { OutsourceLawsuitService } from "~/services/domain-service/outsource-lawsuit-manage-index.service";
import { PageService } from "~/utils/page.service";
import { SortService } from '~/utils/sort.service';
import NumberRange from '~/components/common/number-range.vue';
import DataForm from "~/components/common/data-form.vue";
import DataBox from "~/components/common/data-box.vue";
import CostMaintain from "~/components/outsource-manage/outsource-lawsuit-manage/cost-maintain.vue";
import LawsuitKind from "~/components/lawsuit-follow/lawsuit-kind.vue";
import LawsuitFollow from "~/components/lawsuit-follow/lawsuit-follow.vue";
import { OpenExecutionData } from "~/config/types";
import { ApplyType, CASE_CHANNEL, CASE_HALT_CATEGORY, } from '~/config/enum.config';
import { Mutation, Getter, namespace, State } from 'vuex-class';
const CollectionExecutionModule = namespace('collection-execution');

// @Auth(1011)
@Layout("workspace")
@Component({
  components: {
    NumberRange,
    DataForm,
    DataBox,
    CostMaintain,
    LawsuitKind,
    LawsuitFollow,
  }
})
export default class OutsourceLawsuitManageIndex extends Vue {
  @Dependencies(OutsourceLawsuitService)
  private outsourceLawsuitService: OutsourceLawsuitService;
  @Dependencies(PageService) private pageService: PageService;
  @Dependencies(SortService) private sortService: SortService;
  @CollectionExecutionModule.Action private openCollectionExecution;

  private rowData: any = {};
  private dataList: any[] = [];
  private selectionList: any[] = [];
  private isView: boolean = false;
  private get lawsuit() {
    return {
      kind: this.rowData.lawsuitFollowType,
      status: this.rowData.lawerStatus
    }
  }

  // form 查询表单参数
   private queryModel: any = {
      caseNumber:'',
      personalName:'',
      mobileNo:'',
      idCard:'',
      overdueAmount:{
        max: '',
        min: '',
      },
      overduePeriod:'',
      overdueDays: {
        max: '',
        min: '',
      },
      sysCode:'',
      productName:'',
  }
  private dialog: any = {
    isOpenCostMaintenance: false,
    isOpenLawsuitKind: false,
    isOpenLawsuitFollow: false,
  };

  private mounted() {
    this.refreshData();
  }

  private refreshData() {
    this.outsourceLawsuitService.getCaseOutLawsuitList(this.queryModel,this.pageService)
      .subscribe(data => {
        this.dataList = data
        if(this.dataList){
          this.dataList.forEach(x=>{
            x.overduePeriod>12?x.overduePeriod = 'M12+':x.overduePeriod = 'M'+x.overduePeriod
          })
        }
      });
  }

  private recycleCase(){
    if (this.selectionList.length === 0) {
      this.$message.info('请选择要回收的案件');
      return
    }
    const ids = this.selectionList.map(v => v.id)
    this.outsourceLawsuitService.caseRecover({ids}).subscribe(
      data => {
        this.$message.success(data);
        this.refreshData();
      }
    )
  }

  /**
   * 重置numberRange  
   */ 
  private follFeedback() {
    this.queryModel.outAmount = { min: '', max: '' };
    this.queryModel.overdueDays = { min: '', max: '' };
  }

  /**
   * 点击诉讼跟进
   */
  private lawsuitClick(rowData) {
    this.rowData = rowData
    if(!rowData && rowData.assistStatus === 'ASSIST_OVER'){
      this.$message.info('无诉讼跟进信息');
      return;
    }
    // 如果存在诉讼类型 跳过选择诉讼类型的一步
    if (!rowData.lawsuitFollowType) {
      this.dialog.isOpenLawsuitKind=true
    } else {
      this.dialog.isOpenLawsuitFollow = true
    }
  }

  /**
   * 进入查看执行页
   */
  private openExecutionPage(row) {
    this.openCollectionExecution(new OpenExecutionData(row.caseId, row.caseNumber, CASE_CHANNEL.OUTER, true,true))
  }

}
</script>

<style lang="less">
</style>


